<?php
include '../template.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include '../util/dbutil.php';
getTopWithJS("..");
?>

&nbsp;<br />
	
<?php

	$id = $_GET['id'];
	if(!isset($id)) {
		die('Protein information not available!');
	}

        $ref = $_GET['ref'];

        $getref = "";
        if (!empty($ref))
            $getref = "&ref=$ref";

	// function to format protein sequences
	function formatproteinsequence($seqpro,$sites,$getref) {
		$aaperline = 60;
		$aaperword = 10;
		$naa = strlen($seqpro);
                $naalen = ceil(log10($naa));
		$nwords = ceil($naa/$aaperword);
		$seqnew = "<font face=\"Courier New, Courier, Monaco, ProFont, Monotype Corsiva, Base Monospace\">";
		for ($iwords=0; $iwords<$nwords; $iwords++) {
			$start = $iwords*$aaperword;
                        if ($start%$aaperline==0) {
                                $head = strval($start+1);
                                $head = str_repeat("&nbsp;",$naalen-strlen($head)).$head;
                                $seqnew = $seqnew.$head."&nbsp;";
                        }

                        if ($iwords==$nwords-1) {
                            $end = $naa;
                        } else {
                            $end = $start+$aaperword;
                        }
                        $seqcurr = substr($seqpro,$start,$end-$start);
			
			$nadd = 0;
			while (current($sites) && key($sites)<$end) {
				//$curr = current($sites);
				$loc = key($sites)-$start+$nadd;
				$subst = "<a href=\"phosphosite.php?id=".current($sites).$getref."\">".substr($seqcurr,$loc,1)."</a>";
				$seqcurr = substr_replace($seqcurr,$subst,$loc,1);
				$nadd += strlen($subst)-1;
				next($sites);
			}
			
			$seqnew  = $seqnew.$seqcurr;
			if ($end%$aaperline==0) {
				$seqnew = $seqnew."<br/>";
			} else {
				$seqnew = $seqnew."&nbsp;";
			}
		}
		//$seqnew = $seqnew.substr($seqpro,$nwords*$aaperword)."</font>";
		return $seqnew;
	}

//        $org = $_COOKIE['p3db-organism'];
//        $src = $_COOKIE['p3db-datasource'];
//        echo $org;
//        echo "*".$src;

	// query database for phosphorylation sites in this protein
	$query  = "SELECT id, locationInProtein FROM site WHERE protein = $id AND ptm='p'";
        if (!empty($ref)) {
            $query .= " AND EXISTS (SELECT * FROM mass,peptide,peptideSiteRelation "
                        ."WHERE mass.dataSource=$ref AND mass.id=peptide.mass"
                        ." AND peptideSiteRelation.peptide=peptide.id AND peptideSiteRelation.site=site.id)";
        }
	$result = mysql_query($query) or die("Error, query failed: $query");
	$sites = array();
	while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$sites[$row['locationInProtein']] = $row['id'];
	}
	ksort($sites);

        // query dataSource
        $query = "SELECT id,reference FROM dataSource,proteinDataSourceRelation WHERE protein='$id' AND dataSource=id ORDER BY pubmed DESC";
        $result = mysql_query($query) or die('Error, query failed');
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $dataSource[$row['id']] = $row['reference'];
        }
        if (count($dataSource)>1) {
            $dataSource[0] = "All";
            ksort($dataSource);
        }

        // query spectral count
        $mass = spectralOfProtein($id, $ref);
//        $massLink = "<a href=\"./spectrum.php?id=".implode(',',$mass)."\">".count($mass)."</a>&nbsp;&nbsp;";
        $massLink = count($mass);

	// query database for protein information
	$query  = "SELECT annotation,symbol,sequence,latin,commonName ".
                  "FROM protein,organism ".
                  "WHERE protein.id = '$id' AND protein.organism=organism.label;";
	$result = mysql_query($query) or die("Error, query failed: $query");
	
	// write the data
	echo "<table width=\"100%\">";
	while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                
		echo "<tr><td align=\"right\"><b>Accessions:</b><td>&nbsp;</td></td><td>";
                echo implode('&nbsp;&nbsp;', getXrefLink($id));
                echo "</td></tr>";

		echo "<tr><td align=\"right\"><b>Description:</b></td><td>&nbsp;</td><td>{$row['annotation']}</td></tr>";
                echo "<tr><td align=\"right\"><b>Organism:</b></td><td>&nbsp;</td><td><i>{$row['latin']}</i> ({$row['commonName']})</td></tr>";
                echo "<tr><td align=\"right\" width=\"100\"><b>No. of Sites:</b></td><td>&nbsp;</td><td>".count($sites)."</td></tr>";
                echo "<tr><td align=\"right\" width=\"100\"><b>No. of Spectra:</b></td><td>&nbsp;</td><td>$massLink</td></tr>";

                echo "<tr><td align=\"right\"><b>Reference:</b></td><td>&nbsp;</td><td>";
                echo "<form method=\"get\" action=\"protein.php\">";
                echo "<input name=\"id\" type=\"hidden\" id=\"id\" value=\"$id\" />";
                echo "<select name=\"ref\" size=\"1\">";
                foreach($dataSource as $dsId => $dsRef) {
                        echo "<option value=\"$dsId\"";
                        if ($ref==$dsId) {
                            echo "selected=\"selected\"";
                        }
                        echo ">$dsRef</option>";
                }
		echo "</select>";
                echo "<input name=\"select\" type=\"submit\" id=\"select\" value=\"Select\" /></form>";
                echo "</td></tr>";

                $seqpro = formatproteinsequence($row['sequence'],$sites,$getref);
		echo "<tr><td valign=\"top\" align=\"right\"><b>Sequence:</b></td><td>&nbsp;</td><td>$seqpro</td></tr>";

		echo "<tr><td>&nbsp;</td></tr>";
		
		echo "<tr><td></td><td>&nbsp;</td><td align=\"right\">"; 
		echo "<form method=\"get\" action=\"http://musite.net/\" target=\"_blank\">";
		echo "<input type=\"hidden\" name=\"model\" value=\"Phosphorylation.A.thaliana.general.ser.thr\">";
		echo "<input type=\"hidden\" name=\"seq\" value=\"{$row['sequence']}\">";
		echo "<input name=\"pred\" type=\"submit\" id=\"pred\" value=\"Send to Musite for phosphosite prediction\">";
		echo "</form></td></tr>";
		//echo "<tr><td valign=\"top\" width=\"90\">&nbsp</td><td>&nbsp</td></tr>";
	}
	echo "</table>";
	include '../dbconfig/closedb.php';
?>

	
<?php getBottom(); ?>

	